﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- Write a script that converts a number in the range [0...999] to a text corresponding to its English pronunciation. Examples:
	0 = "Zero"
	273 = "Two hundred seventy three"
	400 = "Four hundred"
	501 = "Five hundred and one"
	711 = "Seven hundred and eleven" -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		//------------------------------------------------------
		// Helper functions
		//------------------------------------------------------
		function digitToString(number, ignoreZero) {
			var result = "";

			// Make integer number
			number = Math.floor(number);

			switch (number) {
				case 0:
					if (!ignoreZero)
					{ result = "zero"; }
					break;
				case 1:
					result = "one";
					break;
				case 2:
					result = "two";
					break;
				case 3:
					result = "three";
					break;
				case 4:
					result = "four";
					break;
				case 5:
					result = "five";
					break;
				case 6:
					result = "six";
					break;
				case 7:
					result = "seven";
					break;
				case 8:
					result = "eight";
					break;
				case 9:
					result = "nine";
					break;
			}

			return result;
		}

		function dozensToString(number)
		{
			var result = "";

			// Make integer number
			var number = Math.floor(number);

			switch (number)
			{
				case 2:
					result = "twenty";
					break;
				case 3:
					result = "thirty";
					break;
				case 4:
					result = "forty";
					break;
				case 5:
					result = "fifty";
					break;
				case 6:
					result = "sixty";
					break;
				case 7:
					result = "seventy";
					break;
				case 8:
					result = "eighty";
					break;
				case 9:
					result = "ninety";
					break;
			}

			return result;
		}

		function to19ToString(number)
		{
			var result = "";

			// Make integer number
			var number = Math.floor(number);

			switch (number)
			{
				case 10:
					result = "ten";
					break;
				case 11:
					result = "eleven";
					break;
				case 12:
					result = "twelve";
					break;
				case 13:
					result = "thirteen";
					break;
				case 14:
					result = "fourteen";
					break;
				case 15:
					result = "fifteen";
					break;
				case 16:
					result = "sixteen";
					break;
				case 17:
					result = "seventeen";
					break;
				case 18:
					result = "eighteen";
					break;
				case 19:
					result = "nineteen";
					break;
			}

			return result;
		}

		function upToHundredToString(number)
		{
			var result = "";
			// Make integer number
			var number = Math.floor(number);

			if (number <= 9)
			{ result = digitToString(number, true); }
			else
			{
				if (number <= 19)
				{ result = to19ToString(number); }
				else
				{
					result = dozensToString(number / 10);
					var remainder = (number % 10);
					if (remainder > 0)
					{ result += " "; }
					result += digitToString(remainder, true);
				}
			}

			return result;
		}
		
		//------------------------------------------------------
		// Main Code
		//------------------------------------------------------
		var number = prompt("Enter number in range [0..999]");
		number *= 1;

		if (!isNaN(number) && number >= 0 && number <= 999) {

			var result = digitToString(number);

			if (number >= 100)
			{
				result = digitToString((number / 100), true) + " hundred";
				
				var remainder = (number % 100);

				if (remainder > 0) {
					result += " ";

					if (remainder < 20)
					{ result += "and "; }
				}

				result += upToHundredToString(remainder);
			}
			else if (number > 9)
			{ result = upToHundredToString(number); }
			else
			{ result = digitToString(number); }

			// Make the first letter upper case
			result = result.substring(0, 1).toUpperCase() + result.substring(1);

			jsConsole.writeLine("The result is: " + result);
		}
		else {
			jsConsole.writeLine("Wrong input.");
		}
	</script>
</body>
</html>
